package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.stat.model.StatActivitySummaryModel;
import com.starbaba.stat.service.StatNewActivitySummaryService;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class StatActivitySummaryNewAction extends BaseAction implements ModelDriven<StatActivitySummaryModel>{

	private StatActivitySummaryModel model;
	private StatNewActivitySummaryService service;
	
	@SuppressWarnings("deprecation")
	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	private InputStream input;

	private String fileName;
	
	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
	@Autowired
	@Qualifier("statNewActivitySummaryServiceImpl")
	public void setService(StatNewActivitySummaryService service) {
		this.service = service;
	}

	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String prodSql = "SELECT * FROM comm_product_prdid p";
		String channelSql = "SELECT * FROM comm_product_channel c";
		try {
			List<CommProductPrdid> prodList = dao.list(prodSql, CommProductPrdid.class);
			List<CommProductChannel> channelList = dao.list(channelSql, CommProductChannel.class);
			List<LoanProductInfo> activityList = dao.list(new LoanProductInfo(), new String[0]);
//			CommProductPrdid prod = new CommProductPrdid();
////			prod.setPrdid(-1);
////			prod.setName("请");
//			prodList.add(prod);
//			Collections.reverse(prodList);
			
//			CommProductChannel channel = new CommProductChannel();
//			channel.setChannel_code(0);
//			channel.setChannel_name("全部");
//			channelList.add(channel);
//			Collections.reverse(channelList);
			
//			LoanProductInfo activity = new LoanProductInfo();
//			activity.setId(0L);
//			activity.setName("全部");
//			activityList.add(activity);
//			Collections.reverse(activityList);
			
			paramsMap.put("prodList", prodList);
			paramsMap.put("channelList", channelList);
			paramsMap.put("activityList", activityList);
		} catch (AppException e) {
			LogService.error(e);
		}
		
		return ActionSupport.SUCCESS;
	}
	
	public void queryList(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		
		try {
			List list = service.getList(start, length, pageResponse, paramsMap);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		String value = service.getExportString(service.getList(null, null, pageResponse, paramsMap));
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
	
	
	
	@Override
	public StatActivitySummaryModel getModel() {
		if (model == null) {
			this.model = new StatActivitySummaryModel();
		}
		return model;
	}
}
